package com.tensquare.friend.controller;

import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Date 2021/3/28 16:11
 */
@RestController
@RequestMapping("friend")
@CrossOrigin
public class FriendController {


    @Autowired
    private FriendService friendService;

    private HttpServletRequest request;
    @PutMapping("/like/{friendid}/{type}}")
    public Result addFriend(@PathVariable String friendid, @PathVariable String type){
        //验证是否登录，并且拿到当前登录用户的id
        Claims claims = (Claims) request.getAttribute("claims_user");
        if(claims==null){
            //没有user权限
            return new Result(false, StatusCode.LOGINERROR, "权限不足");
        }
        //有user权限继续
        String userid = claims.getId();
        //判断添加好友还是非好友
        if(type!=null){
            //添加好友
            if(type.equals("1")){
                int flag=friendService.addFriend(userid,friendid);
                if(flag==0){
                    return new Result(false, StatusCode.ERROR,"不能重复添加好友！");
                }
                else if(flag==1){
                    return new Result(true, StatusCode.OK,"添加成功！");
                }
            }
            //添加非好友
            else{
//                friendService.addNotFriend();
            }
            return new Result(false, StatusCode.ERROR,"参数异常");
        }else{
            return new Result(false, StatusCode.ERROR,"参数异常");
        }
    }
}
